//
//  TZMeVc.m
//  BSBDJ
//
//  Created by user on 16/5/23.
//  Copyright © 2016年 Burning. All rights reserved.
//

#import "TZMeVc.h"
#import "TZActiveCell.h"
#import "TZMeCell.h"
#import "TZLoadAndRegistVc.h"
#import "TZWebController.h"
#import "TZActiveItem.h"
#import <WebKit/WebKit.h>
#import "TZSettingVc.h"



#define cols  4
#define margin 1
@interface TZMeVc ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, weak) UICollectionViewFlowLayout *layout;

@property (nonatomic, strong) NSMutableArray *activeList;
@property (nonatomic, strong) NSMutableArray *fifterList;
@end

@implementation TZMeVc
static NSString *customID = @"customcell";
static NSString *collectionID = @"collectionID";

- (NSMutableArray *)fifterList{
    if (_fifterList == nil) {
        _fifterList = [NSMutableArray array];
    }
    return _fifterList;
}
//懒加载
- (UICollectionView *)collectionView{
    if (_collectionView == nil) {
        
        CGFloat width = (TZScreenW - 3) / cols;
        
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = margin;
        layout.minimumInteritemSpacing = margin;
        layout.itemSize = CGSizeMake(width, width);
        _layout = layout;
        
        UICollectionView *collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, TZScreenW, 0) collectionViewLayout:layout];
        collectionV.delegate = self;
        collectionV.dataSource = self;
        collectionV.scrollEnabled = NO;
        collectionV.backgroundColor =  XMGColor(244, 244, 244);
        _collectionView = collectionV;
    }
    return _collectionView;
}

// 初始化TableView类型
- (instancetype)init{
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
   

    
}
// 设置UI
- (void)setupUI{
    self.view.backgroundColor = XMGColor(244, 244, 244);
    self.title = @"我的";
    [self initTableView];
    [self setupNav];
    [self sendRequeset];
}



//初始化TableView
- (void)initTableView{
    self.tableView.sectionFooterHeight = TZMargin;
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.contentInset = UIEdgeInsetsMake(-TZMargin *2, 0, -20, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"TZMeCell" bundle:nil] forCellReuseIdentifier:customID];
    self.tableView.tableFooterView = self.collectionView;
    [self.collectionView registerNib:[UINib nibWithNibName:@"TZActiveCell" bundle:nil] forCellWithReuseIdentifier:collectionID];
}

- (void)setupNav{
    // 标题
    self.navigationItem.title = @"我的";
    // 右边-设置
    UIBarButtonItem *settingItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"mine-setting-icon"] highImage:[UIImage imageNamed:@"mine-setting-icon-click"] target:self action:@selector(clickSettingBtn)];
    // 右边-月亮
    UIBarButtonItem *moonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"mine-moon-icon"] highImage:[UIImage imageNamed:@"mine-moon-icon-click"] target:self action:@selector(clickMoneBtn)];
    
    self.navigationItem.rightBarButtonItems = @[settingItem, moonItem];
}

- (void)clickMoneBtn
{
    NSLog(@"黑暗系统");

}


- (void)sendRequeset{
    MJWeakSelf
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"a"] = @"square";
    parameters[@"c"] = @"topic";
    [HttpRequestList requestActiveList:requestPath paramaters:parameters success:^(NSArray *activeList) {
        weakSelf.activeList = (NSMutableArray *)activeList;
        if (weakSelf.activeList.count) {
          
            

            [weakSelf.collectionView reloadData];
            
            NSInteger rows = (weakSelf.activeList.count -1 ) / 4 + 1;

            weakSelf.collectionView.height = rows *(weakSelf.layout.itemSize.height +1);
            weakSelf.tableView.tableFooterView = weakSelf.collectionView;
        };
    } failre:^(NSError *error) {
        
    }];
}

// 事件点击
- (void)clickSettingBtn
{
    NSLog(@"设置");
    [self.navigationController pushViewController:[TZSettingVc new] animated:YES];

}

#pragma mark --------------
#pragma mark 数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    static NSString *ID = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    if (indexPath.section == 0){
        cell.imageView.image = [UIImage imageNamed:@"setup-head-default"];
        cell.textLabel.text = @"登陆/注册";
    }else if(indexPath.section == 1){
        cell.textLabel.text = @"离线下载";
    }else if(indexPath.section == 2){
        cell = [TZMeCell customcellWithTableView:tableView];
    }
    
    return cell;
}
// TableView代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        NSLog(@"登陆");
    }else if(indexPath.section == 1){
        NSLog(@"离线下载");
    }
}

#pragma mark --------------
#pragma mark colectionView数据源
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.activeList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TZActiveCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionID forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.activeItem = self.activeList[indexPath.row];
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TZActiveItem *activeItem = self.activeList[indexPath.row];
    if([activeItem.url containsString:@"http"]){
    NSURL *url = [NSURL URLWithString:activeItem.url];
    TZWebController *webVc = [TZWebController new];
    webVc.url = url;
    [self.navigationController pushViewController:webVc animated:YES];
    }
}

- (void)dealloc{
    NSLog(@"delloc----me");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
